﻿//最后一块石头的重量(easy): https://leetcode.cn/problems/last-stone-weight/
class Solution {
public:
    int lastStoneWeight(vector<int>& stones) {
        priority_queue<int> q;
        for(auto x:stones)
        {
            q.push(x);
        }
        while(q.size()>1)
        {
            int a=q.top(); q.pop();
            int b=q.top(); q.pop();
            if(a>b)  q.push(a-b);
        }
        return q.size()==1?q.top():0;
    }
};

//数据流中的第K大元素(easy):https://leetcode.cn/problems/kth-largest-element-in-a-stream/
class KthLargest {
public:
    priority_queue<int,vector<int>,greater<int>> heap; //小根堆
    int _k;
    KthLargest(int k, vector<int>& nums) {
        _k=k;
        for(auto x:nums)
        {
            heap.push(x);
            if(heap.size()>k) heap.pop();
        }
    }
    
    int add(int val) {
        heap.push(val);
        if(heap.size()>_k) heap.pop();
        return heap.top();
    }
};

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest* obj = new KthLargest(k, nums);
 * int param_1 = obj->add(val);
 */